# -*- coding: UTF-8 -*-
# @Create   : 2021/10/8 14:34
# @Author   : yh
# @Remark   : IP设备View层
from mxsoftpy import View

from server.common.NetworkManage.IpAM.Ip import IPServer
from utils.public.params_method import check_params
from index import _


class IPAdd(View):
    def post(self):
        """
        新增IP设备
        """
        try:
            check_params(self.request.POST, ['ip', 'plan_state', 'network', 'subnet'])

            return _('新增IP成功'), IPServer().create(**self.request.POST, company=self.request.company)
        except Exception as e:
            return str(e)


class IPDelete(View):
    def post(self):
        """
        删除IP设备
        """
        check_params(self.request.POST, ['ip', 'network'])

        errors = IPServer().delete(**self.request.POST, company=self.request.company)
        if errors:
            return _('删除ip完成，但有部分ip未成功删除：%s') % str(errors)
        return _('删除ip成功'), ''


class IPRetrieve(View):
    def post(self):
        """
        IP设备详情
        """
        check_params(self.request.POST, ['ip', 'network'])

        return _('获取IP详情成功'), IPServer().retrieve(**self.request.POST, company=self.request.company)


class IPUpdate(View):
    def post(self):
        """
        更新IP设备
        """
        check_params(self.request.POST, ['ip', 'plan_state', 'network', 'subnet'])
        return _('更新IP成功'), IPServer().update(**self.request.POST, company=self.request.company)


class IPSelect(View):
    def post(self):
        """
        条件查询IP设备
        """
        check_params(self.request.POST, ['network', 'subnet'])
        return _('查询IP设备成功'), IPServer().select(**self.request.POST, company=self.request.company)


class IPTrust(View):
    def post(self):
        """
        添加IP信任
        """
        check_params(self.request.POST, ['trusts', 'network'])
        errors = IPServer().trust(company=self.request.company, **self.request.POST)
        if errors:
            return _('添加ip信任完成，但有部分ip未成功添加信任：%s') % str(errors)
        return _('添加ip信任成功'), ''


class IPNoTrust(View):
    def post(self):
        """
        获取未信任的IP数量
        """
        from bsmiddle import GetUnTrustIPCoJson
        return _('获取未信任IP数量成功'), eval(GetUnTrustIPCoJson(self.request.company)).get('count')


class IPList(View):
    def post(self):
        """
        列表展示所有IP
        """
        check_params(self.request.POST, ['network', 'subnet'])
        return _('查询IP成功'), IPServer().list(**self.request.POST, company=self.request.company)
